
$(".topimg").click(function() {
	scrollTo(0, 0);
})
var top_bar = document.getElementsByClassName("top-bar")[0];
var logo_nav = document.getElementsByClassName("logo-nav")[0];
var h = top_bar.clientHeight;
var o = document.getElementsByClassName("o")[0];
var other = document.getElementsByClassName("other")[0];
window.onscroll = function() {
	var jl = document.documentElement.scrollTop || document.body.scrollTop;

	if(jl >= 10) {
		o.style.position = "fixed";
		o.style.top = 0;
		other.style.display = "block"
	} else {
		o.style.position = "static";
		other.style.display = "none"
	}
}
//加载购物车的商品
createGoodsCat();
function createGoodsCat(){
//清空购物车
$("table tbody:eq(1)").html("");
var arr=getJsonObject();
console.log(arr)
if(arr.length==0){
	$(".shopcar.w").css("display","none");
	$(".kong").css("display","block")
	return false;
}
var num=0,zongPrice=0;
 for(var i=0;i<arr.length;i++){
var o=arr[i];
zongPrice+=(o.price*o.num);
num+=o.num;
var tr=$('<tr>'
		+'<td width="60">'
			+'<img src="'+o.imgSrc+'" alt="朵唯L9mini">'
		+'</td>'
		+'<td>'
			+'<a href="/goods/content/201612/289.html" title="朵唯L9mini" target="_blank">'+o.title+'</a>'
			+'<span class="silver">['+o.color+']</span>'
		+'</td>'
		+'<td>￥'+o.price
		+'</td>'
		+'<td class="quantity" width="80">'
		   +' <button class="jian l" onclick="prveAndNextGoods('+o.pId+',-1)">-</button>'
			+'<input type="text" name="quantity" value="'+o.num+'" maxlength="4" class="l">'
			+'<button class="jia l" onclick="prveAndNextGoods('+o.pId+',1)">+</button>'
		+'</td>'
		+'<td width="140">'
			+'<span class="subtotal">￥'+o.price*o.num+'</span>'
		+'</td>'
		+'<td>'
			+'<a href="javascript:delGoods('+o.pId+');" class="delete">删除</a>'
		+'</td></tr>');
		$("table tbody:eq(1)").append(tr);
		//
 }
 $("#effectivePrice").html("￥"+zongPrice+".00元");
 //订单数据
 $(".subOrder ul li:eq(0) span").html(parseInt(Math.random()*100000)+100000);
 $(".subOrder ul li:eq(1) span").html("￥"+zongPrice+".00元");
 $(".shopnum").html(num);

}
//删除商品
 	
// 	提交订单首先要判断是不是登录状态
// 	如果是的就直接查看，不是就跳注册
function delGoods(id){
 var arr=JSON.parse($.cookie("carData"));
 for(var i=0;i<arr.length;i++){
 	if(arr[i].pId==id){
 	  arr.splice(i,1);
 	  break;
 	}
 }
  $.cookie('carData',JSON.stringify(arr), { expires: 7 });
  createGoodsCat();
}
function prveAndNextGoods(id,num){
	var arr=JSON.parse($.cookie("carData"));
	for(var i=0;i<arr.length;i++){
		if(arr[i].pId==id){
			arr[i].num+=num;
			if(arr[i].num<1){
				arr[i].num=1;
			}
			break;
		}
	}
$.cookie('carData',JSON.stringify(arr),{ expires: 7 });
createGoodsCat();
}

function order(){
	if(!$.cookie("uName")){
		alert("亲,你还没有注册账号哦")
		location.href="reg.html"
	}else{
		$(".subOrder").css("display","block");
		$(".submit-step>li:eq(1)").addClass("current");
		$(".submit-step>li:eq(0)").removeClass("current");
	}
	
}

function zhifu(){
var arr=JSON.parse($.cookie("carData"));
	arr.length=0;
	$.cookie('carData',JSON.stringify(arr), { expires: 7 });
	location.href="indexs.html";	
}

function delAllGoods(){
	var arr=JSON.parse($.cookie("carData"));
	arr.length=0;
	$.cookie('carData',JSON.stringify(arr), { expires: 7 });
	createGoodsCat();
}
$(".dis").click(function(){
	$(".subOrder").css("display","none");
	$(".submit-step>li:eq(0)").addClass("current");
	$(".submit-step>li:eq(1)").removeClass("current");
})
